/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-05-07
 * Time: 22:48
 */

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-05-06
 * Time: 14:07
 */
public class Sort {

    private void swap(int[] arr, int x, int y) {
        int tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }

    /**
     * 插入排序
     */
    public void InsertSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - 1;
            while (j >= 0) {
                if (arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                } else {
                    arr[j + 1] = tmp;
                    break;//说明当前有序了,
                }
                j--;
            }
            arr[j + 1] = tmp;
        }
    }

    /**
     * 希尔排序
     */
    public void ShellSort(int[] arr) {
        int gap = arr.length;
        while (gap > 1) {
            gap /= 2;
            shell(arr, gap);
        }
    }

    private void shell(int[] arr, int gap) {
        for (int i = 0; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i - gap;
            while (j >= 0) {
                if (arr[j] > tmp) {
                    arr[j + gap] = arr[j];
                } else {
                    arr[j + gap] = tmp;
                    break;//说明当前有序了,
                }
                j -= gap;
            }
            arr[j + gap] = tmp;
        }
    }

    /**
     * 选择排序
     */
    public void SelectSort(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex = j;
                }
            }
            swap(arr, minIndex, i);
        }
    }

    /**
     * 堆排序
     */

    public void HeapSort(int[] arr) {
        //升序,建大堆
        createBigHeap(arr);
        int end = arr.length - 1;
        while (end >= 0) {
            swap(arr, 0, end);
            shiftDown(arr, 0, end);
            end--;
        }
    }

    private void createBigHeap(int[] arr) {
        for (int parent = (arr.length - 1 - 1) / 2; parent >= 0; parent--) {
            shiftDown(arr, parent, arr.length);
        }
    }

    private void shiftDown(int[] arr, int parent, int end) {
        int child = parent * 2 + 1;
        while (child < end) {

            if (child + 1 < end && arr[child] < arr[child + 1]) {
                child++;
            }
            if (arr[child] > arr[parent]) {
                swap(arr, parent, child);
                parent = child;
                child = parent * 2 + 1;
            } else {
                break;
            }

        }
    }

    /**
     * 快速排序
     */
    public void QuickSort(int[] arr) {
        quick(arr, 0, arr.length - 1);
    }

    private void quick(int[] arr, int left, int right) {
        //递归
        if (left >= right) {
            return;
        }
        int midIndex = midThreeIndex(arr, left, right);
        swap(arr, midIndex, left);
        int pio = partitionK(arr, left, right);
//        int pio = partition(arr, left, right);
        quick(arr, 0, pio - 1);
        quick(arr, pio + 1, right);
    }

    /**
     * hoare法分割
     */
    private int partition(int[] arr, int left, int right) {

        int tmp = arr[left];
        int i = left;
        while (left < right) {
            while (left < right && arr[right] >= tmp) {//必须是>=否则死循环了
                right--;
            }
            while (left < right && arr[left] <= tmp) {
                left++;
            }
            swap(arr, right, left);
        }
        swap(arr, i, left);
        return left;
    }

    /**
     * 挖坑法分割
     */
    private int partitionK(int[] arr, int left, int right) {
        int tmp = arr[left];
        while (left < right) {
            while (left < right && arr[right] >= tmp) {
                right--;
            }
            arr[left] = arr[right];
            while (left < right && arr[left] <= tmp) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = tmp;
        return left;
    }

    /**
     * 三段分法
     */
    private int midThreeIndex(int[] arr, int left, int right) {
        int mid = (left + right) / 2;
        // right left
        if (arr[left] > arr[right]) {
            if (arr[mid] > arr[left]) {
                return left;
            } else if (arr[mid] < arr[right]) {
                return right;
            } else {
                return mid;
            }
        } else {
            // left right
            if (arr[mid] > arr[right]) {
                return right;
            } else if (arr[mid] < arr[left]) {
                return left;
            } else {
                return mid;
            }
        }
    }

    /**
     * 非递归快排,利用栈
     */
    public void QuickSortNoRecursion(int[] arr) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = arr.length - 1;
        int pio = partition(arr, left, right);
        if (pio > left + 1) {
            stack.push(left);
            stack.push(pio - 1);
        }
        if (pio < right - 1) {
            stack.push(pio + 1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            pio = partition(arr, left, right);
            if (pio > left + 1) {
                stack.push(left);
                stack.push(pio - 1);
            }
            if (pio < right - 1) {
                stack.push(pio + 1);
                stack.push(right);
            }
        }
    }

    /**
     * 归并排序
     */

    //递归
    public void mergeSort(int[] arr) {
        merge(arr, 0, arr.length - 1);
    }

    private void merge(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = (left + right) / 2;
        //分解
        merge(arr, left, mid);
        merge(arr, mid + 1, right);
        //合并
        mergeFun(arr, left, mid, right);
    }

    //合并
    private void mergeFun(int[] arr, int left,
                          int mid, int right) {
        int s1 = left;
        int e1 = mid;
        int s2 = mid + 1;
        int e2 = right;
        int k = 0;
        int[] tmp = new int[right - left + 1];
        while (s1 <= e1 && s2 <= e2) {
            if (arr[s1] < arr[s2]) {
                tmp[k++] = arr[s1++];
            } else {
                tmp[k++] = arr[s2++];
            }
        }

        while (s1 <= e1) {
            tmp[k++] = arr[s1++];
        }
        while (s2 <= e2) {
            tmp[k++] = arr[s2++];
        }
        //此时tmp有序了,拷回原数组

        for (int i = 0; i < k; i++) {
            arr[left + i] = tmp[i];
        }
    }

    //非递归
    public void mergeSortN(int[] arr) {
        mergeN(arr);
    }

    //没有分解的过程
    private void mergeN(int[] arr) {
        int gap = 1;
        while (gap <= arr.length) {
            for (int i = 0; i < arr.length; i = i + 2 * gap) {
                int mid = i + gap - 1;
                if (mid >= arr.length) {
                    mid = arr.length - 1;
                }
                int right = mid + gap;
                if (right >= arr.length) {
                    right = arr.length - 1;
                }
                mergeFun(arr, i, mid, right);
            }
            gap *= 2;
        }
    }

}
